# SuperFastPython.com
# example of issuing a task with apply_async() to the process pool and wait for the result
from random import random
from time import sleep
from multiprocessing.pool import Pool
 
# task executed in a worker process
def task():
    # generate a random value
    value = random()
    # report a message
    print(f'Task generated {value}', flush=True)
    # block for a moment
    sleep(1)
    # report a message
    print(f'Task done with {value}', flush=True)
    # return the generated value
    return value
 
# protect the entry point
if __name__ == '__main__':
    # create and configure the process pool
    pool = Pool()
    # issue tasks to the process pool
    for i in range(5):
        result = pool.apply_async(task)
    # wait for the return value
    value = result.get()
    # report the return value
    print(f'Got: {value}')
    # close the process pool
    pool.close()